﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Proggy.Data.Abstract;
using Proggy.Data.Entities;
using Proggy.Web.Cms.Controllers.BackEnd;
using Proggy.Web.Cms.Repositories.Membership;


namespace Proggy.Web.Cms.Core.Controllers.PropertyType
{
    public class ContentPickerController : BackEndControllerBase
    {
        public ContentPickerController(INodeRepository nodeRepo)
        {
            this._repo = nodeRepo;
        }

        private INodeRepository _repo;


        public PartialViewResult ContentPickerItems(int[] items)
        {

            // start model
            var model = new List<KeyValuePair<int, NodeBase>>();

            // check
            if (items != null)
            {

                // iterate
                foreach (var item in items.Distinct())
                {
                    // find
                    var node = _repo.GetPublishableNodes(false).SingleOrDefault(x => x.PublishableNodeId == item);

                    // check
                    if (node != null && node.HasDraft)
                    {
                        model.Add(new KeyValuePair<int, NodeBase>(node.PublishableNodeId, node.DraftNode));
                    }
                    else
                    {
                        if (node != null)
                        {
                            model.Add(new KeyValuePair<int, NodeBase>(node.PublishableNodeId, node));
                        }
                    }

                }
            }

            // 
            return PartialView(model);

        }

        public PartialViewResult ContentPicker()
        {
            return PartialView();
        }
    }
}
